package com.example.smalldimplestore;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.smalldimplestore.adapter.ApplicationState;
import com.example.smalldimplestore.adapter.BaseFragmentAdapter;
import com.example.smalldimplestore.base.BaseActivity;
import com.example.smalldimplestore.databinding.ActivityMainBinding;
import com.example.smalldimplestore.interfalce.MainFragmentProxy;
import com.example.smalldimplestore.ui.IndexFragment;
import com.example.smalldimplestore.view.MainTableView;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;

import androidx.annotation.Nullable;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, MainTableView.OnTableChangedListener {


    ActivityMainBinding binding;
    private ApplicationState applicationState = new ApplicationState();
    private int currentAt = 0;
    private Fragment fragment[] = new Fragment[5];


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.setActivity( this );
        initView();
    }

    private void initView() {
        BaseFragmentAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager());
        fragment[0] = new IndexFragment();
        fragment[1] = new IndexFragment();
        fragment[2] = new IndexFragment();

        fragmentAdapter.addData(fragment);
        binding.viewPager.setAdapter(fragmentAdapter);
        binding.viewPager.setCurrentItem(currentAt);
        binding.viewPager.setOnPageChangeListener(this);
        binding.mainTableView.setOnTableChangedListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (binding.mainTableView != null) {
            binding.mainTableView.onClick(binding.viewPager.getCurrentItem());
        }
        if (applicationState.isMainSliding() && position == 1) {
            applicationState.setMainSliding(false);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (!applicationState.isMainSliding() && state != ViewPager.SCROLL_STATE_IDLE) {
            applicationState.setMainSliding(true);
        }
    }

    @Override
    public void onTableChanged(int index) {
        if (currentAt != index) {
            currentAt = index;
            binding.viewPager.setCurrentItem(index, false);
        } else {
            if (fragment[index] instanceof MainFragmentProxy) {
                ((MainFragmentProxy) fragment[index]).scrollToTop();
            }
        }
    }
}
